home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11142 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  50 lines

  1. Path: news2.noc.netcom.net!news
  2. From: Tarang Deshpande <tarang@willows.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie needs help w/ recursion
  5. Date: Tue, 19 Mar 1996 16:54:13 -0800
  6. Organization: NETCOM Network Operations
  7. Message-ID: <314F5735.6E96@willows.com>
  8. References: <4ikq6p$io1@impsets.dash.com>
  9. NNTP-Posting-Host: daffy.willows.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. Jim Bosshardt wrote:
  16. > Hi, I am new and am about to pull my hair out over the supposedly simple
  17. > recursion problem I have. The function is to take a number and raise it
  18. > to a neg or pos power and return the value to main().  I need to take
  19. > the following and make it a recursive function...
  20. > double power (double a, float b)
  21.  
  22.  
  23. Note the change that I've made to the parameter list.
  24.  
  25. double power ( double a, long b )
  26. {
  27.  
  28.     if ( ! a )
  29.     /*--- This is actually an error ---*/
  30.         return ( 0 );
  31.  
  32.     if ( ( ! b ) || ( b == -1 ) )
  33.         return ( 1 );
  34.  
  35.     if ( b == 1 )
  36.         return ( a );
  37.     
  38.     if ( b > 0 )
  39.         return ( a * power ( a, b - 1 ) );
  40.  
  41.     if ( b < 0 )
  42.         return ( 1 / ( a * power ( a, b + 1 ) ) );
  43.  
  44. }
  45.  
  46.  
  47. Of course you could just use the pow function instead.
  48.